package com.to3ks.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.to3ks.common.base.result.R;
import com.to3ks.eduservice.entity.EduTeacher;
import com.to3ks.eduservice.entity.vo.EduTeacherQueryVo;
import com.to3ks.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Tigger
 * @since 2021-01-06
 */

@Api(description = "讲师管理")
@RestController
@RequestMapping("/admin")
@Slf4j
public class EduTeacherController {


    @Autowired
    private EduTeacherService   eduTeacherService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R listAll(){
        List<EduTeacher> list = eduTeacherService.list();
        //List<EduTeacher> list = eduTeacherService;
        return R.ok().data("items", list);
    }

    @ApiOperation("讲师分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                      @ApiParam("讲师列表查询对象") EduTeacherQueryVo teacherQueryVo){

        Page<EduTeacher> pageParam = new Page<>(page, limit);
        IPage<EduTeacher> pageModel = eduTeacherService.selectPage(pageParam, teacherQueryVo);
        List<EduTeacher> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("根据id选出指定教师")
    @GetMapping("get/{id}")
    public R getTeacher(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        if(teacher != null){
            return R.ok().data("item", teacher);
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(value = "讲师对象", required = true) @RequestBody EduTeacher teacher){
        eduTeacherService.save(teacher);
        return R.ok().message("保存成功");
    }

    @ApiOperation(value = "根据ID列表删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(
            @ApiParam(value = "讲师ID列表", required = true)
            @RequestBody List<String> idList){
        log.info("***********removeRows方法执行"+idList.toString());
        boolean result = eduTeacherService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("更新讲师")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "讲师对象", required = true) @RequestBody EduTeacher teacher){
        boolean result = eduTeacherService.updateById(teacher);
        if(result){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "根据ID删除讲师", notes = "根据ID删除讲师，逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){

        //删除讲师
        boolean result = eduTeacherService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

}

